package com.wn.class30.service.impl;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.BusinessDao;
import com.wn.class30.mapper.ProductDao;
import com.wn.class30.mapper.ProductImgDetailDao;
import com.wn.class30.mapper.ProductImgHeadDao;
import com.wn.class30.service.ShoppingMallHomeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingMallHomeServiceImpl implements ShoppingMallHomeService {
    @Resource
    ProductDao productDao;
    @Resource
    private BusinessDao businessDao;
    @Override
    public List<Product> getProduct() {
        return productDao.selectByExample(null);
    }

    @Override
    public List<Product> getProductWithNew(Long offset, Integer limit) {
        ProductExample example = new ProductExample();
        example.setOffset(offset);
        example.setLimit(limit);
        List<Product> list = productDao.selectByExample(example);
        return list;
    }

    @Override
    public Long getTotal() {
        long l = productDao.countByExample(null);
        return l;
    }

    @Override
    public List<Product> getProductWithHot() {
        List<Product> product = this.getProduct();
        List<Product> hot = new ArrayList<Product>();
        for (Product product1 : product) {
            if("1".equals(product1.getProductIshot())){
                hot.add(product1);
            }
        }
        return hot;
    }

    @Resource
    ProductImgHeadDao productImgHeadDao;
    @Override
    public List<ProductImgHead> getProductImgHeadByProductId(Integer productId) {
        ProductImgHeadExample productImgHeadExample = new ProductImgHeadExample();
        ProductImgHeadExample.Criteria criteria = productImgHeadExample.createCriteria();
        //倒序
//        productImgHeadExample.setOrderByClause("product_img_head_sort desc");
        //顺序
        productImgHeadExample.setOrderByClause("product_img_head_sort");
        criteria.andProductIdEqualTo(productId);
        List<ProductImgHead> productImgHeads = productImgHeadDao.selectByExample(productImgHeadExample);
        return productImgHeads;
    }

    @Override
    public Product getProductByProductId(Integer productId) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(productId);
        return productDao.selectByExample(example).get(0);
    }

    @Resource
    ProductImgDetailDao productImgDetailDao;
    @Override
    public List<ProductImgDetail> getProductImgDetailByProductId(Integer productId) {
        ProductImgDetailExample example = new ProductImgDetailExample();
        ProductImgDetailExample.Criteria criteria = example.createCriteria();
        //顺序
        example.setOrderByClause("product_detail_sort");
        ProductImgDetailExample.Criteria criteria1 = criteria.andProductIdEqualTo(productId);
        return productImgDetailDao.selectByExample(example);
    }

    /**
     * 根据商品id查询商家详情
     * @param productId
     * @return
     */
    @Override
    public List<Business> getProductBusinessByProductId(Integer productId) {
        return businessDao.selectBusinessByProductId(productId);
    }

    @Override
    public Business selectBusinessByBusinessId(Integer businessId) {
        return businessDao.selectByPrimaryKey(businessId);
    }

    @Override
    public List<Product> selectProductsByBusinessId(Integer businessId) {

        return productDao.selectProductsByBusinessId(businessId);
    }

}
